package logic.util;

import java.lang.reflect.Type;

import logic.bean.ChapterInfo;
import logic.constants.Const;

import org.apache.http.Header;
import org.geometerplus.android.util.QyreaderPerferencesUtil;

import android.R.integer;
import android.content.Context;

import com.google.gson.reflect.TypeToken;
import com.shwread.http.base.ObjectHttpResponseHandler;
import com.shwread.http.base.TextHttpResponseHandler;


/**
 * @author xuzhijun
 * @date 2015年5月19日下午4:35:38
 */
public class ResponseHandlerForChapterInfo
        extends
        ObjectHttpResponseHandler<ChapterInfo>
{
	private Context                                context;
	private String                                 bookId, chapterId;
	private int                                    chapterIndex = -1;
	
	private ObjectHttpResponseHandler<ChapterInfo> handlerO;
	private TextHttpResponseHandler                handlerT;
	
	private boolean                                isO;
	
	public ResponseHandlerForChapterInfo(ObjectHttpResponseHandler<ChapterInfo> handler, Context context, String bookId, String chapterId, int chapterIndex) {
		this.context = context;
		this.bookId = bookId;
		this.chapterId = chapterId;
		this.chapterIndex = chapterIndex;
		this.handlerO = handler;
		isO = true;
	}
	
	public ResponseHandlerForChapterInfo(ObjectHttpResponseHandler<ChapterInfo> handler, Context context, String bookId, String chapterId) {
		this.context = context;
		this.bookId = bookId;
		this.chapterId = chapterId;
		this.handlerO = handler;
		isO = true;
	}
	
	public ResponseHandlerForChapterInfo(TextHttpResponseHandler handler, Context context, String bookId, String chapterId, int chapterIndex) {
		this.context = context;
		this.bookId = bookId;
		this.chapterId = chapterId;
		this.chapterIndex = chapterIndex;
		this.handlerT = handler;
		isO = false;
	}
	
	public ResponseHandlerForChapterInfo(TextHttpResponseHandler handler, Context context, String bookId, String chapterId) {
		this.context = context;
		this.bookId = bookId;
		this.chapterId = chapterId;
		this.handlerT = handler;
		isO = false;
	}
	
	@Override
	public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
		if (isO)
		{
			handlerO.onFailure(statusCode, headers, responseString, throwable);
		}
		else
		{
			handlerT.onFailure(statusCode, headers, responseString, throwable);
		}
	}
	
	@Override
	public void onSuccess(int statusCode, Header[] headers, ChapterInfo object, String responseString) {
		if (method(object))
		{
			if (isO)
			{
				handlerO.onSuccess(statusCode, headers, object, responseString);
			}
			else
			{
				handlerT.onSuccess(statusCode, headers, responseString);
			}
		}
		else
		{
			if (isO)
			{
				handlerO.onFailure(statusCode, headers, responseString, null);
			}
			else
			{
				handlerT.onFailure(statusCode, headers, responseString, null);
			}
		}
		
	}
	
	@Override
	public Type getType() {
		return new TypeToken<ChapterInfo>()
		{
		}.getType();
	}
	
	private boolean method(ChapterInfo info) {
		if (null != info && logic.util.Util.isNotEmpty(info.getContent()))
		{
			writeOnlineXML(bookId, chapterId + ".html", info.getContent());
			if (chapterIndex >= 0) QyreaderPerferencesUtil.getInstance(context, Const.phone_number).setDownloadChapter(bookId, chapterIndex + 1);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean writeOnlineXML(String contentId, String chapterId, String xml) {
		return FileUtil.writeOnlineXML(contentId, chapterId, xml);
	}
}
